<?php

	class mySql	{
		var $c;
		var $host;
		var $user;
		var $pass;
		var $db;
		var $debug;

		function mySql($host, $user, $pass, $db=false) { $this->__construct($host, $user, $pass, $db); }

		function __construct($host, $user, $pass, $db=false) {
			$this->host = $host;
			$this->user = $user;
			$this->pass = $pass;
			$this->db = $db;
			if(!$this->c = mysql_connect($host, $user, $pass)) {
				return false;
			}
			if($db) {
			  $this->query("use $db");
			}
		}

		function __wakeup() {
			$this->__construct(
				$this->host,
				$this->user,
				$this->pass,
				$this->db
			);
		}

		function close() {
			return mysql_close($this->c);
		}

		// $fields is an array with key as column name
		function insert($table,$fields) {
			if(is_array($fields)) {
	    		foreach($fields as $k=>$v) {
	        		if($n++) { $keys .= ","; $values .= ","; }
	        		$keys .= $k;
	        		$values .= "'". mysql_escape_string($v)."'";
	    		}
	    		return "insert into $table ($keys) values ($values)";
			} else {
				return false;
			}
		}

		// $fields is an array with key as column name
		// $where can be int, this causes 'update [table] set ... where id = [where]'
		// $where can be array, this makes a full where clause with key as column
		function update($table,$fields,$where) {
			$sql = "update $table set ";
			foreach($fields as $k=>$v) {
			if($n++) $sql .= ",";
			    $sql .= "$k = ".$this->qq($v);
			}
			$sql .= " where ";
			if(is_array($where)) {
			    foreach($where as $x=>$y) {
				if($klask++) $sql .= " and ";
				$sql .= " $x = ".$this->qq($y);
			    }
			} elseif(is_int($where)) {
			    $sql .= " id = ".$this->qq($where);
			} else
				return false;
			return $sql;
		}

		// $where can be int, this causes 'delete from [table] where id = [where]'
		// $where can be array, this makes a full where clause with key as column
		function delete($table,$where) {
			$sql = "delete from $table where ";
			if(is_array($where)) {
			    foreach($where as $x=>$y) {
				if($klask++) $sql .= " and ";
				$sql .= " $x = ".$this->qq($y);
			    }
			} elseif(is_int($where)) {
			    $sql .= " id = ".$this->qq($where);
			} else
				return false;
			return $sql;
		}

		function qq($var) {
			return "'".mysql_escape_string($var)."'";
		}
		
		function query($sql) {
			$sql = trim($sql);
			if (!is_resource($this->c)) {
				echo "<hr><pre>";
				var_dump(debug_backtrace());
			}
			if($this->debug) echo "$sql<br>";
			$r = mysql_query($sql, $this->c);
			
			if($r == false) {
				return false;
			} 
			// XXX: this is a little eeky
			switch(false) {
				case strncasecmp("insert", $sql, 6):
					return mysql_insert_id($this->c);
					break;
				case strncasecmp("update", $sql, 6):
					return mysql_affected_rows($this->c);
					break;
				case strncasecmp("delete", $sql, 6):
					return mysql_affected_rows($this->c);
					break;
				default:
					return new mySqlResult($r, $this->c);
			}
		}
		
		function getOneRow($sql) {
			if($foo = $this->query($sql))
				return $foo->fetch();
			else
				return false;
		}
		
		function getOneValue($sql) {
			if($foo = $this->getOneRow($sql))
				return reset($foo);
			else
				return false;
		}
		
		function getColArray($sql, $colname, $idcol=false) {
			if($res = $this->query($sql)) {
				while($ar = $res->fetch()) {
					if($idcol) {
						$ret[$ar[$idcol]] = $ar[$colname];
					} else {
						$ret[] = $ar[$colname];
					}
				}
				return $ret;
			}
			return false;
		}

		function sqlList($result) {
			$return = "(";
			$c = false;
			foreach($result as $v) {
				if($c) $return .= ",";
				$return .= "$v";
				$c = true;
			}
			$return .= ")";
			return $return;
		}

		function setDebug($bool = false) {
			$this->debug = $bool;
		}
	}

	class mySqlResult extends mySql {
		var $r;

		function mySqlResult($res, $con) { $this->__construct($res, $con); }

		function __construct($res, $con) {
			$this->c = $con;
			$this->r = $res;
		}

		function numrows() {
			return mysql_num_rows($this->r);
		}

		function fetch() {
			return mysql_fetch_assoc($this->r);
		}
		
	}
